package com.farewell.hotel.controller;

import com.farewell.hotel.entity.InfoSearch;
import com.farewell.hotel.pojo.Order;
import com.farewell.hotel.service.OrderService;
import com.farewell.hotel.service.RoomService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @author Farewell
 * @date 2021/4/23
 */
@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private RoomService roomService;

    @RequestMapping("deleteOrder.do")
    @ResponseBody
    public Boolean deleteOrder(@RequestParam("id") Long id) {
        try {
            orderService.deleteOrderById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }


    @RequestMapping("findCost.do")
    @ResponseBody
    public List<Order> findCost(@RequestBody InfoSearch orderInfoSearch, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        if (orderInfoSearch.getConditionType() == null || orderInfoSearch.getKeyword() == null) {
            orderInfoSearch = null;
        }
        if (orderInfoSearch != null && (orderInfoSearch.getConditionType().isEmpty() || orderInfoSearch.getKeyword().isEmpty())) {
            orderInfoSearch = null;
        }
        return orderService.findCost(orderInfoSearch);
    }

    @RequestMapping("outRoom.do")
    @ResponseBody
    public Boolean outRoom(@RequestBody Order order) {
        try {
            orderService.addOrder(order);
            roomService.setRoomToCleaning(order.getRoomId());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @RequestMapping(value = "findOutRoomInfoByRoomId.do", method = RequestMethod.POST)
    @ResponseBody
    public Map findOutRoomInfoByRoomId(@RequestParam("roomId") Long roomId) {
        Order order = orderService.findOutRoomInfoByRoomId(roomId);
        try {
            Map describe = BeanUtils.describe(order);
            return describe;
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            e.printStackTrace();
            return null;
        }
    }
}
